package com.dlc.felear.xiaoensale.utils;


import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.factory.CmdList;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DeviceStatusSuper {
    private static String TAG = "DeviceStatusSuper";
    private static String OUPUTIOSERIAL = "ouputioserial";
    private static String STEPMOTORSERIAL = "stepMotorSerial";
    private static String HOTSERIAL = "hotSerial";
    private static String SOCKETTIMEOUT = "hotSerial";
    private static String WEIGHT_ = "weight_";
    private static DeviceStatusSuper instance;
    private int timeOut = 3000;
    private ScheduledExecutorService scheduledThreadPool;
    private HashMap<String, Long> records = new HashMap<>();
    private int type = 0;
    private int poundSize = 4;

    public void setType(int type) {
        Log.e(TAG, "type=" + type);
        this.type = type;
    }

    private DeviceStatusSuper() {
        scheduledThreadPool = new ScheduledThreadPoolExecutor(1);
        initData();
        startTimer();
    }

    private void initData() {
        records.put(OUPUTIOSERIAL, System.currentTimeMillis());
        records.put(STEPMOTORSERIAL, System.currentTimeMillis());
        records.put(HOTSERIAL, System.currentTimeMillis());
        records.put(SOCKETTIMEOUT, System.currentTimeMillis());
    }

    private void startTimer() {
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    checkCCJQRAllFlag();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 5, 5, TimeUnit.SECONDS);
    }

    public static DeviceStatusSuper getInstance() {
        if (instance == null) {
            synchronized (DeviceStatusSuper.class) {
                if (instance == null) {
                    instance = new DeviceStatusSuper();
                }
            }
        }
        return instance;
    }

    public void recieveHotData(byte[] data) {
        Log.e(TAG,"recieveHotData:"+HexUtil.bytes2HexString(data));
        if (CmdList.checkSum16(data) || CmdList.checkDataComplete(data)) {
            records.put(HOTSERIAL, System.currentTimeMillis());
        }
    }

    public void recieveSocketData(String msg) {
        records.put(SOCKETTIMEOUT, System.currentTimeMillis());
    }

    public void recieveWeightData(int num) {
        records.put(WEIGHT_ + num, System.currentTimeMillis());
    }

    public void recieveJbData(byte[] data) {
        if (CmdList.checkDataComplete(data)) {
            Log.e(TAG, "recieveJbData " + HexUtil.bytes2HexString(data));
            records.put(STEPMOTORSERIAL, System.currentTimeMillis());
        }
    }

    public void recieveIoData(byte[] data) {
        if (CmdList.checkDataComplete(data)) {
            records.put(OUPUTIOSERIAL, System.currentTimeMillis());
        }
    }


    private void checkYdcAllFlag() {
        String tempMsg = "";
        if (System.currentTimeMillis() - records.get(HOTSERIAL) > 2000) {
            tempMsg = "串口通讯异常，请联系管理员";
        }
        showMsgForDialog(tempMsg);
    }

    private void checkPctAllFlag() {
        String tempMsg = "";
        Log.e(TAG, "checkPctAllFlag:" + (System.currentTimeMillis() - records.get(SOCKETTIMEOUT)));
        if (System.currentTimeMillis() - records.get(SOCKETTIMEOUT) > 6000 && type == 2) {
            tempMsg = "网络异常，请联系管理人";
        } else {
            for (int i = 1; i <= poundSize; i++) {
                String key = WEIGHT_ + i;
                if (records.containsKey(key) && System.currentTimeMillis() - records.get(key) > timeOut) {
                    tempMsg = "";
                } else {
                    tempMsg = "第" + i + "传感器异常，请联系管理员";
                    break;
                }
            }
        }
        showMsgForDialog(tempMsg);
    }

    private void checkCCJQRAllFlag() {
        String tempMsg = "";
        if (System.currentTimeMillis() - records.get(OUPUTIOSERIAL) > timeOut && type == 0) {
            tempMsg = "下位机控制板通讯异常";
        }
        if (System.currentTimeMillis() - records.get(HOTSERIAL) > timeOut && type == 0) {
            tempMsg = "电磁加热机芯通讯异常";
        }
        Log.e(TAG,"time:"+(System.currentTimeMillis() - records.get(HOTSERIAL)));
        showMsgForDialog(tempMsg);
    }

    private void showMsgForDialog(String msg) {
        Log.e(TAG, "监控信息:" + msg);
        EventBus.getDefault().post(msg, Constant.DEVICE_ERROR_MSG_SHOW);
    }
}
